<template>
  <div class="scroll-wrapper" ref="wrapper">
    <div class="scroll-inner" :style="{ minHeight: height + 1 + 'px' }">
      <div :class="['scroll-pulldown', isPullingDown ? 'active' : '']" v-if="pulldownActive" ref="pulldown">
        <div class="scroll-pulldown-inner">
          <i class="scroll-pulldown-icon"></i>
          <p>{{ pulldownTip }}</p>
          <p>更新时间:{{ nowTime }}</p>
        </div>
      </div>
      <slot></slot>
      <div :class="['scroll-pullup', isisPullingUpTip ? 'active' : '']" v-if="pullupActive" ref="pulldown">
        <div class="scroll-pullup-inner">
          <i class="scroll-pullup-icon"></i>
          <p>{{ pullupTip }}</p>
          <p>更新时间:{{ nowTime }}</p>
        </div>
      </div>
      <div v-else style="text-align: center">{{ tiptxt }}</div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  props: {
    /**
     * 滚动区域高度
     */
    height: {
      type: [String, Number],
      default: 0,
      required: true,
    },
    /**
     * 1 会非实时（屏幕滑动超过一定时间后）派发scroll 事件。
     * 2 会在屏幕滑动的过程中实时的派发 scroll 事件。
     * 3 除了实时派发scroll事件，在swipe的情况下仍然能实时派发scroll事件
     */
    probeType: {
      type: Number,
      default: 0,
    },
    /**
     * 是否有回弹动画
     */
    bounce: {
      type: Boolean,
      default: true,
    },
    /**
     * 点击列表是否派发click事件
     */
    click: {
      type: Boolean,
      default: true,
    },
    /**
     * 是否开启横向滚动
     */
    scrollX: {
      type: Boolean,
      default: false,
    },
    /**
     * 是否启用下拉刷新
     */
    pulldownActive: {
      type: Boolean,
      default: true,
    },
    /**
     * 是否启用上拉加载更多
     */
    pullupActive: {
      type: Boolean,
      default: true,
    },
    tiptxt: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      isPullingDown: false,
      isPullingUp: false,
      isisPullingUpTip: false,
      pulldownTip: '下拉刷新数据',
      pullupTip: '上拉加载更多',
    }
  },
  computed: {
    nowTime() {
      var myDate = new Date()
      return myDate.getHours() + ':' + myDate.getMinutes()
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.wrapper.style.height = this.height + 'px'
      this._initScroll()
    })
  },
  methods: {
    _initScroll() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        bounce: this.bounce,
        probeType: this.probeType,
        click: this.click,
        scrollX: this.scrollX,
        pullDownRefresh: this.pulldownActive,
        pullUpLoad: this.pullupActive,
      })
      //eslint-disable-next-line
            this.scroll.on('touchEnd', pos => {
        if (this.isPullingUp && this.pullupActive && this.scroll.y <= this.scroll.maxScrollY - 60) {
          this.isisPullingUpTip = true
          this.pullupTip = '数据正在加载'
          this.$emit('pullingUp')
          // console.log(2)
        }
      })
      //eslint-disable-next-line
            this.scroll.on('scroll', (pos) => {
        if (!this.isPullingDown && this.pulldownActive) {
          this.pulldownTip = '释放刷新数据'
        }
      })

      this.scroll.on('scrollEnd', () => {
        if (this.isPullingDown && this.pulldownActive) {
          this.isPullingDown = false
          this.pulldownTip = '下拉刷新数据'
        }
        if (this.isPullingUp && this.pullupActive) {
          this.isPullingUp = false
          this.isisPullingUpTip = false
          this.pullupTip = '上拉加载更多'
          this.scroll.finishPullUp()
        }
        this.scroll.refresh()
      })

      if (this.pulldownActive) {
        this._initPullDownRefresh()
      }

      if (this.pullupActive) {
        this._initPullUpLoad()
      }
    },
    _initPullDownRefresh() {
      this.scroll.on('pullingDown', () => {
        this.isPullingDown = true
        this.pulldownTip = '正在刷新数据'
        this.$emit('pullingDown')
        this.scroll.finishPullDown()
      })
    },
    _initPullUpLoad() {
      this.scroll.on('pullingUp', () => {
        this.pullupTip = '释放加载数据'
        this.isPullingUp = true
      })
    },
    disable() {
      // 代理better-scroll的disable方法
      this.scroll && this.scroll.disable()
    },
    enable() {
      // 代理better-scroll的enable方法
      this.scroll && this.scroll.enable()
    },
    refresh() {
      // 代理better-scroll的refresh方法
      this.scroll && this.scroll.refresh()
    },
    scrollTo() {
      // 代理better-scroll的scrollTo方法
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    },
    scrollToElement() {
      // 代理better-scroll的scrollToElement方法
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
    },
  },
  watch: {
    height() {
      this.$refs.wrapper.style.height = this.height + 'px'
      this.scroll && this.scroll.refresh()
    },
  },
  destroyed() {
    this.$refs.scroll && this.$refs.scroll.destroy()
  },
}
</script>

<style>
.scroll-wrapper {
  overflow: hidden;
}
.scroll-inner {
  position: relative;
}
.scroll-pulldown {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  transform: translateY(-100%);
  text-align: center;
}
.scroll-pulldown-inner {
  position: relative;
  display: inline-block;
  padding: 5px;
}
.scroll-pulldown-inner p {
  height: 25px;
  line-height: 25px;
  font-size: 14px;
  margin: 0;
  padding: 0;
}
.scroll-pullup {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  transform: translateY(100%);
  text-align: center;
}
.scroll-pullup-inner {
  position: relative;
  display: inline-block;
  padding: 5px;
}
.scroll-pullup-inner p {
  height: 25px;
  line-height: 25px;
  font-size: 14px;
  margin: 0;
  padding: 0;
}
.scroll-pulldown-icon,
.scroll-pullup-icon {
  width: 32px;
  height: 32px;
  position: absolute;
  left: -40px;
  top: 50%;
  margin-top: -16px;
}
.scroll-pulldown-icon {
  background: url('');
  background-size: 32px;
}
.scroll-pullup-icon {
  background: url('');
  background-size: 32px;
}
.scroll-pulldown.active .scroll-pulldown-icon,
.scroll-pullup.active .scroll-pullup-icon {
  background: url('');
  background-size: 32px;
  animation: circle 1.5s infinite linear;
  -webkit-animation: circle 1.5s infinite linear;
}
@keyframes circle {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes circle {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
</style>
